package com.szl.yeb.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.szl.yeb.mapper.AdminRoleMapper;
import com.szl.yeb.entity.AdminRole;
import com.szl.yeb.service.AdminRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.szl.yeb.common.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.szl.yeb.utils.MybatisPlusUtil;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * (AdminRole)表服务实现类
 *
 * @author szl
 * @since 2022-08-28 16:43:55
 */
@Service("adminRoleService")
public class AdminRoleServiceImpl extends ServiceImpl<AdminRoleMapper, AdminRole> implements AdminRoleService {

    @Autowired
    private AdminRoleMapper adminRoleMapper;

    @Override
    public PageResult getPage(Integer currentPage, Integer size, AdminRole adminRole) {
        Page<AdminRole> page = new Page<>(currentPage, size);

        QueryWrapper<AdminRole> queryWrapper = MybatisPlusUtil.queryWrapperBuilder(adminRole);

        this.page(page, queryWrapper);

        return new PageResult((int) page.getTotal(), page.getRecords());
    }

    @Override
    @Transactional
    public Integer updateAdminRole(String adminId, List<Integer> rids) {
        adminRoleMapper.delete(MybatisPlusUtil.getLQWrapper(AdminRole.class).eq(AdminRole::getAdminId,adminId));
        if(rids == null || rids.isEmpty()){
            return 1;
        }
        return adminRoleMapper.insertAdminRole(adminId,rids);
    }
}

